package com.api.service.v1;

import com.admin.entity.*;
import com.admin.mapper.GroupMapper;
import com.admin.mapper.StationMapper;
import com.admin.mapper.TerminalMapper;
import com.admin.mapper.ThemeMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 终端
 */

@SuppressWarnings("ALL")
@Service
public class TerminalApiService extends ServiceImpl<TerminalMapper, Terminal> {

    @Autowired
    private ThemeMapper themeMapper;

    @Autowired
    private StationMapper stationMapper;

    @Autowired
    private GroupMapper groupMapper;

    /**
     * 根据mac地址查找终端对象
     *
     * @param mac
     * @return
     */
    public Terminal findTerminalByMac(String mac) {
        QueryWrapper q = new QueryWrapper();
        q.eq("terminal_mac", mac);
        IPage result = super.page(new Page(1, 1), q);
        long total = result.getTotal();
        if (total == 0)
            return null;

        return (Terminal) result.getRecords().get(0);
    }

    /**
     * 根据mac地址查找主题
     * @param mac
     * @return
     */
    public Theme findThemeByMac(String mac) {
        //找终端
        Terminal t = findTerminalByMac(mac);
        if (t == null)
            return null;
        //找分组
        Integer gid = t.getTerminalGroup();
        if (gid == null)
            return null;
        TGroup group = groupMapper.selectById(gid);
        if (group==null){
            return null;
        }

        Integer themeId = group.getGroupDesktopConfiguration();
        if (themeId==null)
        {
            return null;
        }
        //找主题
        Theme theme = themeMapper.selectById(themeId);


        return theme;
    }

    /**
     * 根据mac地址所在站点
     * @param mac
     * @return
     */
    public Station  findStationByMac(String mac)
    {
        QueryWrapper q = new QueryWrapper();
        q.eq("terminal_mac", mac);
        IPage<Terminal> result = super.page(new Page(1, 1), q);
        long total = result.getTotal();
        if (total == 0)
            return null;
        Terminal terminal = result.getRecords().get(0);
        Integer  station  = terminal.getTerminalStation();
        if (station!=null)
        {
            return stationMapper.selectById(station);
        }

        return null;

    }

    /**
     * 获取默认主题
     * @return
     */
    public Theme getDefaultTheme(String packageName) {
        QueryWrapper q = new QueryWrapper();
        q.isNotNull("theme_admin");
        q.eq("theme_status","启用");
        if (packageName!=null && packageName.indexOf("hotel")>-1) {
            q.eq("theme_name", "酒店默认主题");
        }
        else
        {
            q.eq("theme_name", "足浴默认主题");
        }
        IPage<Theme> result = themeMapper.selectPage(new Page(1,1),q);
        if (result!=null && result.getTotal()>0)
        {
            return result.getRecords().get(0);
        }
        QueryWrapper q2 = new QueryWrapper();
        q2.isNotNull("theme_admin");
        q2.eq("theme_status","启用");
        result = themeMapper.selectPage(new Page(1,1),q2);
        if (result!=null && result.getTotal()>0)
        {
            return result.getRecords().get(0);
        }

        return null;

    }
}
